/* ****************************************************************** **
**    OpenSees - Open System for Earthquake Engineering Simulation    **
**          Pacific Earthquake Engineering Research Center            **
**                                                                    **
**                                                                    **
** (C) Copyright 1999, The Regents of the University of California    **
** All Rights Reserved.                                               **
**                                                                    **
** Commercial use of this program without express permission of the   **
** University of California, Berkeley, is strictly prohibited.  See   **
** file 'COPYRIGHT'  in main directory for information on usage and   **
** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
**                                                                    **
** Developed by:                                                      **
**   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
**   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
**   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
**                                                                    **
** ****************************************************************** */

// Written: User-defined implementation for UDP Recorder
// Purpose: This file contains the class definition for UDP_Recorder.
// UDP_Recorder is a class for sending node displacement responses via UDP
// for hybrid simulation applications.

#ifndef UDP_Recorder_h
#define UDP_Recorder_h

#include <Recorder.h>
#include <ID.h>
#include <Vector.h>
#include <Domain.h>
#include <Node.h>
#include <actor/message/Message.h>
#ifdef _WIN32
    #include <winsock2.h>
    #include <ws2tcpip.h>
    typedef int ssize_t;
#else
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
#endif

class UDP_Recorder : public Recorder
{
  public:
    UDP_Recorder(int nodeTag, int dof, const char* serverIP, int port);
    ~UDP_Recorder();

    virtual int record(int commitTag, double timeStamp);
    virtual int flush();
    virtual int restart(void);
    virtual int domainChanged(void);
    virtual int setDomain(Domain &theDomain);
    virtual int sendSelf(int commitTag, Channel &theChannel);
    virtual int recvSelf(int commitTag, Channel &theChannel, FEM_ObjectBroker &theBroker);
    virtual void Print(OPS_Stream &s, int flag = 0);

  protected:

  private:
    int theNodeTag;
    int theDOF;
    char* serverAddress;
    int serverPort;
    
    Domain *theDomain;
    Node *theNode;
    
#ifdef _WIN32
    SOCKET sockfd;
    WSADATA wsaData;
#else
    int sockfd;
#endif
    
    struct sockaddr_in servaddr;
    bool socketInitialized;
    
    int initializeSocket();
    int sendData(double value);
    void cleanupSocket();
};

#endif